package com.interceptor;

import com.model.Const;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object arg2) throws Exception {

		String path = request.getServletPath();
		if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
			return true;
		} else {
			HttpSession session = request.getSession();
			String userName = (String) session.getAttribute(Const.SESSION_USERNAME);
			if (userName != null) {
				return true;
			} else {
				response.sendRedirect(request.getContextPath() + "/login.jsp");
				return false;
			}
		}

	}
}
